package uit.edu.vn.Utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Activity.R;
import uit.edu.vn.Model.myImage;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageListAdapter extends BaseAdapter {

	private Context context;
	private List<myImage> listImage;

	private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

	public ImageListAdapter(Context context, List<myImage> listImage) {

		// TODO Auto-generated constructor stub
		this.context = context;
		this.listImage = listImage;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listImage.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listImage.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final myImage entry = listImage.get(position);
		LayoutInflater inflater = LayoutInflater.from(context);
		convertView = inflater.inflate(R.layout.row_image, null);

		TextView txt_ImageName = (TextView) convertView
				.findViewById(R.id.txt_ImageName);
		TextView txt_ImageSize = (TextView) convertView
				.findViewById(R.id.txt_ImageSize);
		ImageView txt_ImageView = (ImageView) convertView
				.findViewById(R.id.ImageView);
		// get dữ liệu in imageName
		txt_ImageName.setText(entry.getName());
		// get dữ liệu  in ImageSize
		txt_ImageSize.setText(String.valueOf(entry.getSize()));
		// convert resource to bitmap
		String pathName = entry.getPath();
		try {
			File imageFile = new File(pathName);
			Bitmap bmp;
			bmp = decodeFile(imageFile);
			bmp = Bitmap.createScaledBitmap(bmp, 50, 50, true);
			txt_ImageView.setImageBitmap(bmp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// xy lu check box
		CheckBox cbox = (CheckBox) convertView
				.findViewById(R.id.chkSelectImage);
		cbox.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectImage);

				if (cb.isChecked()) {
					// itemChecked.set(position, true);
					entry.setChecked(true);

				} else if (!cb.isChecked()) {
					// itemChecked.set(position, false);
					entry.setChecked(false);

				}

			}
		});

		cbox.setChecked(entry.getChecked());

		return convertView;
	}

	// Hàm decodefile nham tranh loi tran bo dem
	private Bitmap decodeFile(File f) {
		try {
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);

			final int REQUIRED_SIZE = 50;
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp / 2 < REQUIRED_SIZE
						|| height_tmp / 2 < REQUIRED_SIZE)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale *= 2;
			}
			// decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
		} catch (FileNotFoundException e) {
		}
		return null;
	}

	// hàm lấy setcheck va get check de lay itemChecklist
	public ArrayList<Boolean> getCheckList() {
		return itemChecked;
	}

	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck) {
		itemChecked = itemCheck;
		return itemChecked;
	}
}